/**
 * @author yxw
 * date : 2013年10月28日 下午1:01:55 
 */
package com.tzinfo.tzt.activity;

import java.util.HashMap;
import java.util.Map;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.TextView;

import com.tzinfo.tzt.activity.view.MyProgressDialog;
import com.tzinfo.tzt.config.AppDataConfig;
import com.tzinfo.tzt.util.HttpTools;
import com.tzinfo.tzt.R;

public class FjhDynamicDetailActivity extends Activity implements
		OnClickListener {
	private String fjhDynamicId = "";
	private Dialog dialog;

	private String webViewString = "";
	private String pubTime = "";

	// UI组件
	private Button backBtn;
	private TextView titleTextName, pubTimeText;
	private WebView contentWebView;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_news_detail);
		Intent thisIntent = this.getIntent();
		fjhDynamicId = thisIntent.getStringExtra("fjhDynamicId");
		pubTime = thisIntent.getStringExtra("pubTime");
		dialog = new MyProgressDialog(this).createDialog(getResources()
				.getString(R.string.loading));
		// 加载进度条
		dialog.show();
		init();

	}

	private void init() {
		titleTextName = (TextView) this.findViewById(R.id.text_title);
		titleTextName.setText("动态详情");
		contentWebView = (WebView) this.findViewById(R.id.webview_news_content);
		pubTimeText = (TextView) this.findViewById(R.id.text_news_pub_time);
		pubTimeText.setText(pubTime);
		backBtn = (Button) this.findViewById(R.id.bt_back);
		backBtn.setOnClickListener(this);
		// 加载数据
//		contentWebView.getSettings().setDefaultTextEncodingName("utf-8");
		contentWebView.loadUrl(AppDataConfig.fjhUrl
				+ AppDataConfig.FjhNewsWebView + "?id=" + fjhDynamicId);
		
//		new Thread(getNewHouseNewsDetialRun).start();
	}

	private void updateUI() {
		contentWebView
				.loadData(webViewString, "text/html; charset=UTF-8", null);
	}

	@SuppressLint("HandlerLeak")
	Handler handler = new Handler() {
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			switch (msg.arg1) {
			case 1:
				updateUI();
				dialog.dismiss();
				break;
			case -1:
				getDataError((String) msg.obj);
				break;
			case 0:
				break;
			}
		}
	};

	/**
	 * 线程 得到新房列表
	 */
	Runnable getNewHouseNewsDetialRun = new Runnable() {

		@Override
		public void run() {
			Message msg = handler.obtainMessage();
			String jsonString;
			String url = AppDataConfig.fjhUrl + AppDataConfig.FjhNewsWebView
					+ "?id=" + fjhDynamicId;
			Log.e("url", url);
			jsonString = HttpTools.getJsonContent(url, "gb2312");
			Log.e("JsonString", jsonString);
			webViewString = jsonString;
			if (webViewString != null && !"".equals(webViewString)) {
				msg.arg1 = 1;
			} else {
				msg.arg1 = -1;
			}
			handler.sendMessage(msg);
		}
	};

	private void getDataError(String ERRORMESSAGE) {
		new AlertDialog.Builder(this).setCancelable(false)
				.setMessage(ERRORMESSAGE)
				.setPositiveButton(getResources().getString

				(R.string.cancel), new DialogInterface.OnClickListener()

				{
					public void onClick

					(DialogInterface dialog, int which) {
						finish();
					}
				}).setNegativeButton(getResources().getString

				(R.string.try_again), new DialogInterface.OnClickListener()

				{
					public void onClick(

					DialogInterface dialogInterface, int

					whichButton) {
						dialog.show();
						new Thread(getNewHouseNewsDetialRun).start();
					}
				}).show();
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.bt_back:
			this.finish();
			break;

		default:
			break;
		}

	}
}
